import { getStore, setStore, clearStore } from '@/utils/store'
import { logout } from '@/api/user'

const user = {
	state: {
		userInfo: getStore({ name: 'USER_INFO' }) || null,
		token: getStore({ name: 'TOKEN' }) || ''
	},
	mutations: {
		"SET_TOKEN": (state, token) => {
			state.token = token
			setStore({
				name: 'TOKEN',
				content: state.token
			})
		},
		"SET_USER_INFO": (state, userInfo) => {
			state.userInfo = userInfo
			setStore({
				name: 'USER_INFO',
				content: state.userInfo
			})
		}
	},
	actions: {
		LogOut({ commit }) {
			return new Promise((resolve, reject) => {
				logout()
					.then(() => {
						commit('SET_TOKEN', '')
						commit('SET_USER_INFO', null)
						clearStore()
						resolve()
					})
					.catch(error => {
						reject(error)
					})
			})
		},
	}
}
export default user
